/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Login;


import UserBN.Authentication;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author lab1
 */
@ManagedBean
@SessionScoped
public class Bean {

    /** Creates a new instance of Bean */
    //phan nay danh cho login
    private String account;
    private String password;
    private boolean remember;

    public Bean() {
        checkCookie();
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void checkCookie() {
        HttpServletRequest httpServletRequest =
                (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        Cookie[] cookies = httpServletRequest.getCookies();
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                if (cookies[i].getName().equalsIgnoreCase("password")) {
                    password = cookies[i].getValue();
                }
                if (cookies[i].getName().equalsIgnoreCase("account")) {
                    account = cookies[i].getValue();
                }

            }
        }
    }
    private Users us;

    public Users getUs() {
        return us;
    }

    public boolean isRemember() {
        return remember;
    }

    public void setRemember(boolean remember) {
        this.remember = remember;
    }

    public String commandLind() {
        return "index";
    }
    public String checkLogin() {
//        us = BOL.checkLogin(account, password);
//        if (us != null) {
//            if (remember) {
//                HttpServletResponse httpServletResponse =
//                (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
//                Cookie cookieaccount = new Cookie("account", account);
//                //Cookie cookieaccount = new Cookie("account", "");
//                cookieaccount.setMaxAge(365);
//                Cookie cookiepassword = new Cookie("password", password);
//                //Cookie cookiepassword = new Cookie("password","");
//                cookieaccount.setMaxAge(365);
//                httpServletResponse.addCookie(cookieaccount);
//                httpServletResponse.addCookie(cookiepassword);
//
//            } else {
//                HttpServletResponse httpServletResponse =
//                        (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
//                Cookie cookieaccount = new Cookie("account", "");
//                cookieaccount.setMaxAge(0);
//                Cookie cookiepassword = new Cookie("account", "");
//                cookieaccount.setMaxAge(0);
//                httpServletResponse.addCookie(cookieaccount);
//                httpServletResponse.addCookie(cookiepassword);
//            }
////            HttpSession session=(HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(false);
////            session.invalidate();
////            session.setAttribute("user",us);
//            return "success";
//        } else {
//            return "fails";
//        }
//        
        Authentication a = Authentication.getInstance();
        if(a.login(account, password) != null)
        {
            return null;
        }else
        {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "System Error",  "Please try again later.");  
            FacesContext.getCurrentInstance().addMessage(null, message);  
            return "";
        }
        
        
        
        
        
        
        
        
        
        
    }

    //phan nay danh cho changedpassword
    private String oldpassword;
    private String newpassword;
    public String changPassword()
    {
        return null;
    }

    public String getNewpassword() {
        return newpassword;
    }

    public void setNewpassword(String newpassword) {
        this.newpassword = newpassword;
    }

    public String getOldpassword() {
        return oldpassword;
    }

    public void setOldpassword(String oldpassword) {
        this.oldpassword = oldpassword;
    }
    public String chanePass()
    {
        int a=BOL.changePassword(us.getAccount(),newpassword);
        if(a==1)
        {
            return "chanepasssuccess";
        }else{
            return "chanepassfails";
        }
    }
}
